package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.JsonObject;
import com.ja.openchat.adapters.LatestCallRecordAdapter;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.call.CallManager;
import com.zy.openchat.core.model.call.RemoteCallInfo;
import com.zy.openchat.core.model.user.UserInfo;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

public class LatestCallFragment extends AbsRecordFragment<RemoteCallInfo> {

    public static LatestCallFragment newInstance(@IntRange(from = 0,to = 2) int recordType){
        LatestCallFragment fragment = new LatestCallFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("recordType",recordType);
        fragment.setArguments(bundle);
        return fragment;
    }

    private int recordType;

    @Override
    public void initData() {
        super.initData();
        initDataAndEvent();
    }

    protected void initDataAndEvent() {
        if(getArguments()!=null){
            recordType = getArguments().getInt("recordType");
        }
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                RemoteCallInfo callInfo = mAdapter.getItem(position);
                if(callInfo==null){
                    return;
                }
                UserInfo targetInfo = null;
                if (UserInfo.getUserInfo().id == callInfo.from){
                    if(callInfo.to!=null&&callInfo.to.size()>0){
                        targetInfo = UserInfo.getUserInfo(callInfo.to.get(0));
                    }
                }else{
                    targetInfo = UserInfo.getUserInfo(callInfo.from);
                }
                if(targetInfo!=null){
                    CallManager.startC2CNewCall(getActivity(),targetInfo,callInfo.isVideo);
                }

            }
        });
    }

    @Override
    protected BaseQuickAdapter<RemoteCallInfo, BaseViewHolder> newIntanceAdapter() {
        return new LatestCallRecordAdapter(recordType);
    }

    @Override
    protected void requestData(int page) {
        TelegramManager.getTelegramManager().queryHistoryCall(getContext(),recordType, currentPage,PAGE_COUNT, new TgResultBlock<List<RemoteCallInfo>>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, List<RemoteCallInfo> obj) {
                finishRefreshAndLoad(page,obj);

            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                finishRefreshAndLoad(page,null);
            }
        });
    }

}
